import java.util.concurrent.CountDownLatch;

//难点通过倒计时来确定最后实际转账的金额，通过设定线程的执行时间来降低并发量减少冲突
public class RunTrans {
    private Account a2;
    private Account a3;
    private Bank bank;

    public static void main(String[] args) {
        final Bank bank = new Bank();
        final Account a2 = new Account("622260026000260001072101");
        a2.setMoney(0);
        final Account a3 = new Account("622260026000260001085202");
        a3.setMoney(20000);
        int numTrans = 15;
        CountDownLatch cl = new CountDownLatch(numTrans);
        Waiting w = new Waiting(cl);
        new Thread(w).start();
        int sum=0;
        for(int i=0;i<numTrans;i++) {
            int rand = (int) (Math.random() * 1000);
            new Thread() {
                    public void run() {
                        try{
                            bank.transferMoney(a3, a2, 10 * rand);
                         }catch (RuntimeException e){
                             System.out.println(e);
                          }finally {
                             cl.countDown();
                             w.setAccount(a2.getMoney());
                             w.setFrom(a3);
                         }
                    }
                }.start();
                sum += 10 * rand;

        }
        w.setSum(sum);
    }




}
